const express = require('express')
const fs = require('fs')
const Game = require(process.cwd() + '/src/class/index')

let router = express.Router()

// 添加或删除一个Autotile
const doAction = async (req) => {
  const env = new Game.Env()
  
  await env.init(req.body.env)
  
  await env.autotileTrigger(req.body.autotile)
  
  // 保存更改,因为更改只涉及读取Autotile文件,所以不用额外压缩数据
  await env.save()
  
  // 因为保存会压缩数据,所以再解压返回前端
  await env.unzip()
  return env
}

/* GET users listing. */
router.post('/', function (req, res) {
  doAction(req).then((data) => {
    res.json(data)
  })
  .catch((err) => {
    console.log(err)
    res.sendStatus(500)
  })
})

module.exports = router
